package com.ruoyi.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    public static String DEFAULT_FORMAT = "yyyy-MM-dd";
    public static String getLastDayStartDate(String time) {
        SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = foramt.parse(time);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, -1);//当前时间减一天

        return foramt.format(cal.getTime()) + " 00:00:00";
    }

    /**
     * 根据传入时间 获取昨天的结束时间
     * @param time
     * @return
     */
    public static String getLastDayEndDate(String time) {
        SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = foramt.parse(time);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar  cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, -1);//当前时间减一天

        return foramt.format(cal.getTime()) + " 23:59:59";
    }

    public static String getLastDayDate() {
        SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd");
        Calendar  cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, -1);//当前时间减一天

        return foramt.format(cal.getTime());
    }

    public static String getDayDate(int count) {
        SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd");
        Calendar  cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, count);

        return foramt.format(cal.getTime());
    }

    //今天开始时间
    public static Date getStartTime(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    //今天结束时间
    public static Date getEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }



    /**
     *  根据传入时间 获取上周一时间
     * @param time
     * @return
     */
    public static String getLastWeekMonday(String time) {
        SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = foramt.parse(time);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar  cal = Calendar.getInstance();
        cal.setTime(date);
        if (1 == cal.get(Calendar.DAY_OF_WEEK)) {//判断当前日期是否为周末，因为周末是本周第一天，如果不向后推迟一天的到的将是下周一的零点，而不是本周周一零点
            cal.add(Calendar.DATE, -1);
        }
        cal.add(Calendar.DAY_OF_MONTH, -7);//时间减去7天
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);//Calendar.MONDAY 这个是周一的意思      想获取周几就更换这个

        return foramt.format(cal.getTime()) + " 00:00:00";
    }

    /**
     * 根据传入时间 获取上周周天时间
     * @param time
     * @return
     */
    public static String getLastWeekSunday(String time) {
        SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = foramt.parse(time);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar  cal = Calendar.getInstance();
        cal.setTime(date);

        if (1 == cal.get(Calendar.DAY_OF_WEEK)) {//判断当前日期是否为周末，因为周末是本周第一天，如果不向后推迟一天的到的将是下周一的零点，而不是本周周一零点
            cal.add(Calendar.DATE, -1);
        }
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);//Calendar.SUNDAY 周天想获取周几就更换这个
        return foramt.format(cal.getTime()) + " 23:59:59";
    }

    //获取传入日期的周一时间
    public static String getThisWeekMonday(Date date) {
        SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return foramt.format(cal.getTime())+" 00:00:00";
    }




    /**
     *  根据传入时间 获取上月第一天的一时间
     * @param time
     * @return
     */
    public static String getLastMonthFirstDate(String time) {
        SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        String monthFirstDate = "";
        try {
            date = foramt.parse(time);
            Calendar c = Calendar.getInstance();
            //设置为指定日期
            c.setTime(date);
            //指定日期月份减去一
            c.add(Calendar.MONTH, -1);
            //指定日期月份减去一后的 获取第一天
            c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
            //获取最终的时间
            Date lastDateOfPrevMonth = c.getTime();
            monthFirstDate =  foramt.format(lastDateOfPrevMonth)+" 00:00:00";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return monthFirstDate;
    }



    /**
     *  当前时间 获取N个月前的指定时间格式
     * @param
     * @return
     */
    public static String getNMonthForamt(int month,String foramtStr) {
        SimpleDateFormat foramt = new SimpleDateFormat(foramtStr);
        Date date = new Date();
        String monthFirstDate = "";

        //date = foramt.parse(time);
        Calendar c = Calendar.getInstance();
        //设置为指定日期
        c.setTime(date);
        //指定日期月份减去一
        c.add(Calendar.MONTH, -month);
        //指定日期月份减去一后的 获取第一天
        //c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
        //获取最终的时间
        Date lastDateOfPrevMonth = c.getTime();
        monthFirstDate =  foramt.format(foramtStr);

        return monthFirstDate;
    }

    /**
     *  根据传入时间 获取上月最后一天一时间
     * @param time
     * @return
     */
    public static String getLastMonthEndDate(String time) {
        SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        String monthFirstDate = "";
        try {
            date = foramt.parse(time);
            Calendar c = Calendar.getInstance();
            //设置为指定日期
            c.setTime(date);
            //指定日期月份减去一
            c.add(Calendar.MONTH, -1);
            //指定日期月份减去一后的 最大天数
            c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
            //获取最终的时间
            Date lastDateOfPrevMonth = c.getTime();
            monthFirstDate =  foramt.format(lastDateOfPrevMonth)+" 23:59:59";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return monthFirstDate;
    }



    /**
     * 格式化日期
     * @param date 日期对象
     * @return String 日期字符串
     */
    public static String formatDate(Date date){
        SimpleDateFormat f = new SimpleDateFormat(DEFAULT_FORMAT);
        String sDate = f.format(date);
        return sDate;
    }

    /**
     * 获取当年的第一天
     * @param year
     * @return
     */
    public static Date getCurrYearFirst(){
        Calendar currCal=Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearFirst(currentYear);
    }

    /**
     * 获取当年的最后一天
     * @param year
     * @return
     */
    public static Date getCurrYearLast(){
        Calendar currCal=Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearLast(currentYear);
    }

    /**
     * 获取某年第一天日期
     * @param year 年份
     * @return Date
     */
    public static Date getYearFirst(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 获取某年最后一天日期
     * @param year 年份
     * @return Date
     */
    public static Date getYearLast(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();

        return currYearLast;
    }


    //得到当前时间和本周开始时间

    public static Date[] getNowAndMonday() throws ParseException {
        SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date[] dates = new Date[2];
        Date nowTime = new Date();
        Date thisWeekMonday = foramt.parse(getThisWeekMonday(nowTime));
        dates[0] = thisWeekMonday;
        dates[1] = nowTime;
        return dates;
    }

    public static String past7Days(){
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, - 7);
        Date d = c.getTime();
        return sd.format(d);
    }

    //本月开始时间
    public static Date getMonthStart(){
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    public static int getQuarterByMonthValue(LocalDate date) {
        int monthValue = date.getMonthValue(); // 1-12
        return (monthValue + 2) / 3; // 整数除法自动取整
    }






}
